package gameObjects;

import com.jme.image.Texture;
import com.jme.image.Texture.MagnificationFilter;
import com.jme.image.Texture.MinificationFilter;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

/**
 * Classe che visualizza il messaggio COMPLETD
 * @author Massimiliano Palermo
 *
 */
public class Completed extends Node{
	private static final long serialVersionUID = 18042010191046L;
	private Quad			panel;
	private int 			x1,y1,x2,y2;
	private DisplaySystem 	display;
	private static Renderer renderer;
	private Text   			coin;
	private Node			txtNode;
	private StringBuffer    strBuf;
	private String			strCoin;
	
	private final Texture	t1;
	private final TextureState ts;
	
	@SuppressWarnings("static-access")
	public Completed()
	{
		super("HUD");
		this.display=DisplaySystem.getDisplaySystem();
		renderer = display.getRenderer();
		
		//Carico le texture
		ts = display.getRenderer().createTextureState();
		t1  = TextureManager.loadTexture(
									this.getClass().getClassLoader().getResource("textures/completed.png"),
									MinificationFilter.Trilinear,
									MagnificationFilter.Bilinear,
									1.0f, true);
		
		//Creo l'oggetto
		x1=display.getWidth()/2; y1=display.getHeight()/2; x2=500; y2=200;
		panel = new Quad("PANEL",(float)x2,(float)y2);
		panel.setRenderQueueMode(renderer.QUEUE_ORTHO);
		panel.setLightCombineMode(Spatial.LightCombineMode.Off);
		panel.setLocalTranslation(x1,y1,0.0f);
		panel.updateRenderState();
		
		//Imposto il materiale per il rendering dell'oggetto.
		MaterialState ms = renderer.createMaterialState();
        ms.setAmbient(new ColorRGBA(1,1,1,0));
        ms.setDiffuse(new ColorRGBA(0,0,0,1));
        ms.setEnabled(true);
        panel.setRenderState(ms);
		
		//Applico la texture
		applyTexture();
		this.attachChild(panel);
		
		strBuf=new StringBuffer(10);
		strBuf.setLength(0);
		strCoin="";
		strBuf.append(strCoin);
		
		coin = new Text();
		coin = Text.createDefaultTextLabel( "COIN label" );
        coin.setCullHint(CullHint.Never);
        coin.setTextureCombineMode(TextureCombineMode.Replace);
		coin.setRenderQueueMode(renderer.QUEUE_ORTHO);
		coin.setLightCombineMode(Spatial.LightCombineMode.Off);
		coin.setLocalTranslation(new Vector3f(250, y1-16, 0));
		coin.print(strBuf);
		
		txtNode=new Node();
		txtNode.setRenderState(coin.getRenderState(RenderState.StateType.Blend));
        txtNode.setRenderState(coin.getRenderState(RenderState.StateType.Texture));
        txtNode.attachChild(coin);
        txtNode.setCullHint(CullHint.Never);
        this.attachChild(txtNode);
        
        txtNode.updateGeometricState(0.0f, true );
        txtNode.updateRenderState();
	}
	
	/**
	 * Imposta la trasparenza dell'HUD
	 * NB: la texture deve avere una zona trasparente!!!
	 */
	private void setUpTransparency()
	{
		BlendState blendState = display.getRenderer().createBlendState();
		blendState.setBlendEnabled( true );
		blendState.setSourceFunction( BlendState.SourceFunction.SourceAlpha );
		blendState.setDestinationFunction( BlendState.DestinationFunction.OneMinusSourceAlpha );
		blendState.setTestEnabled( true );
		blendState.setTestFunction( BlendState.TestFunction.GreaterThanOrEqualTo );
		blendState.setEnabled( true );
		
		panel.setRenderState(blendState);
	}
	
	public void setCoinText(String text)
	{
		strBuf.setLength(0);
		strBuf.append(text);
		coin.print(strBuf);
		coin.updateRenderState();
	}
	
	public void applyTexture()
	{	
		ts.setTexture(t1);
		ts.getTexture().getImage().getWidth(); 
		ts.getTexture().getImage().getHeight();
		ts.setEnabled(true);
		setUpTransparency();
		panel.setRenderState(ts);
	}
	
	public void hide()
	{
		this.removeFromParent();
		this.updateRenderState();
	}
	
}